﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ShopOnline
{
    /// <summary>
    /// TODO:Put a description here.
    /// </summary>
    [Serializable]
    public class NewsRec
    {
        #region Private Members
        private Int64 m_id = 0;
        private Int64 m_domainId = 0;
        private String m_name = String.Empty;
        private String m_imgUrl = String.Empty;
        private String m_description = String.Empty;
        private String m_content = String.Empty;
        private DateTime m_createDate = DateTime.MinValue;
        private int m_status = -1;
        #endregion

        #region Public Properties
        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public Int64 id
        {
            get { return m_id; }
            set { m_id = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public Int64 domainId
        {
            get { return m_domainId; }
            set { m_domainId = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public String name
        {
            get { return m_name; }
            set { m_name = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public String imgUrl
        {
            get { return m_imgUrl; }
            set { m_imgUrl = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public String description
        {
            get { return m_description; }
            set { m_description = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public String content
        {
            get { return m_content; }
            set { m_content = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public DateTime createDate
        {
            get { return m_createDate; }
            set { m_createDate = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public int status
        {
            get { return m_status; }
            set { m_status = value; }
        }

        #endregion

        #region  Properties Const
        private const string ConnectName = "ShopOnline";
        private const string Folderlogs = "News";
        #endregion

        #region Store procedure
        private String usp_GetList_News = "usp_GetList_News";
        private String usp_GetSingle_News = "usp_GetSingle_News";
        private String usp_Add_News = "usp_Add_News";
        private String usp_Delete_News = "usp_Delete_News";
        private String usp_Update_News = "usp_Update_News";
        
        #endregion

        #region Init
        public NewsRec() { }
        public NewsRec(Int64 id, Int64 domainId, String name, String imgUrl, String description, String content, DateTime createDate, int status)
        {
            m_id = id;
            m_domainId = domainId;
            m_name = name;
            m_imgUrl = imgUrl;
            m_description = description;
            m_content = content;
            m_createDate = createDate;
            m_status = status;
        }
        #endregion

        #region Call Store Procedure
        public List<NewsRec> GetList(Int64 Domainid,int PageSize, int PageNumber, ref int Total)
        {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "GetAll - PageSize:" + PageSize + ",PageNumber: " + PageNumber;
            try
            {
                List<NewsRec> records = new List<NewsRec>();
                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_GetList_News,Domainid, PageSize, PageNumber);

                 #region Default variables
                    Int64 id = 0;
                    Int64 domainid = 0;
                    String name = String.Empty;
                    String imgurl = String.Empty;
                    String description = String.Empty;
                    String content = String.Empty;
                    DateTime createdate = DateTime.MinValue;
                    int status = -1;
                #endregion

                if (dsResult.Tables.Count == 2)
                {
                   
                    foreach (DataRow row in dsResult.Tables[0].Rows)
                    {
                        if (row.IsNull("id") == false)
                            id = (Int64)row["id"];
                        if (row.IsNull("domainId") == false)
                            domainid = (Int64)row["domainId"];
                        if (row.IsNull("name") == false)
                            name = row["name"].ToString();
                        if (row.IsNull("imgUrl") == false)
                            imgurl = row["imgUrl"].ToString();
                        if (row.IsNull("description") == false)
                            description = row["description"].ToString();
                        if (row.IsNull("content") == false)
                            content = row["content"].ToString();
                        if (row.IsNull("createDate") == false)
                            createdate = (DateTime)row["createDate"];
                        if (row.IsNull("status") == false)
                            status = (Byte)row["status"];
                        records.Add(new NewsRec(id, domainid, name, imgurl, description, content, createdate, status));
                    }


                    Total = (int)dsResult.Tables[1].Rows[0][0];
                }



                if (records.Count <= 0)
                {
                    iFlagError = 1;
                    sMgsError = "Empty";
                }

                return records;
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return new List<NewsRec>();
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }

        }

        public NewsRec GetSingle(Int64 DomainID,Int64 NewsID)
        {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "GetSingle - DomainID:" + DomainID + ", NewsID:" + NewsID;
            try
            {
                NewsRec objNews = new NewsRec();

                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_GetSingle_News, DomainID, NewsID);

                if (dsResult.Tables[0].Rows.Count > 0)
                {
                    objNews.id = Convert.ToInt64(dsResult.Tables[0].Rows[0]["id"].ToString());
                    if (!dsResult.Tables[0].Rows[0].IsNull("domainId"))
                    {
                        objNews.domainId = (Int64)dsResult.Tables[0].Rows[0]["domainId"];
                    }
                    if (!dsResult.Tables[0].Rows[0].IsNull("name"))
                    {
                        objNews.name = dsResult.Tables[0].Rows[0]["name"].ToString();
                    }
                    if (!dsResult.Tables[0].Rows[0].IsNull("imgUrl"))
                    {
                        objNews.imgUrl = dsResult.Tables[0].Rows[0]["imgUrl"].ToString();
                    }
                    if (!dsResult.Tables[0].Rows[0].IsNull("description"))
                    {
                        objNews.description = dsResult.Tables[0].Rows[0]["description"].ToString();
                    }
                    if (!dsResult.Tables[0].Rows[0].IsNull("content"))
                    {
                        objNews.content = dsResult.Tables[0].Rows[0]["content"].ToString();
                    }
                    if (!dsResult.Tables[0].Rows[0].IsNull("createDate"))
                    {
                        objNews.createDate = (DateTime)dsResult.Tables[0].Rows[0]["createDate"];
                    }
                    if (!dsResult.Tables[0].Rows[0].IsNull("status"))
                    {
                        objNews.status = (int)dsResult.Tables[0].Rows[0]["status"];
                    }

                }
                else
                {
                    iFlagError = 1;
                    sMgsError = "Empty";
                    objNews = null;
                }

                return objNews;
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return null;
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }

        }

        public ObjectResult AddNews(NewsRec objNews) {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "AddNews - NewsID:" + objNews.id;
            try
            {
                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_Add_News,objNews.id,objNews.domainId,objNews.name,objNews.imgUrl,objNews.description,objNews.content);

                int iFlag = -1;
                string sDescription = string.Empty;

                iFlag = (int)dsResult.Tables[0].Rows[0]["Flag"];
                sDescription = dsResult.Tables[0].Rows[0]["Description"].ToString();
                bool bFlag = true;
                if (iFlag != 0)
                {
                    bFlag = false;
                    iFlagError = 1;
                    sMgsError = sDescription;
                }

                return new ObjectResult(bFlag, sDescription);
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return new ObjectResult(false, "Lỗi không xác định");
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }
        }

        public ObjectResult UpdateNews(NewsRec objNews)
        {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "UpdateNews - NewsID:" + objNews.id;
            try
            {
                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_Update_News, objNews.id, objNews.domainId, objNews.name, objNews.imgUrl, objNews.description, objNews.content);

                int iFlag = -1;
                string sDescription = string.Empty;

                iFlag = (int)dsResult.Tables[0].Rows[0]["Flag"];
                sDescription = dsResult.Tables[0].Rows[0]["Description"].ToString();
                bool bFlag = true;
                if (iFlag != 0)
                {
                    bFlag = false;
                    iFlagError = 1;
                    sMgsError = sDescription;
                }

                return new ObjectResult(bFlag, sDescription);
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return new ObjectResult(false, "Lỗi không xác định");
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }
        }

        public ObjectResult DeleteNews(Int64 NewsID, Int64 DomainID) {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "UpdateNews - NewsID:" + NewsID + ", DomainID:" + DomainID;
            try
            {
                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_Delete_News, NewsID,DomainID);

                int iFlag = -1;
                string sDescription = string.Empty;

                iFlag = (int)dsResult.Tables[0].Rows[0]["Flag"];
                sDescription = dsResult.Tables[0].Rows[0]["Description"].ToString();
                bool bFlag = true;
                if (iFlag != 0)
                {
                    bFlag = false;
                    iFlagError = 1;
                    sMgsError = sDescription;
                }

                return new ObjectResult(bFlag, sDescription);
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return new ObjectResult(false, "Lỗi không xác định");
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }
        }
        #endregion

    }
}
